package javanio.channel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SocketChannel;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-09-21
 * Time: 上午10:51
 * Description: ${description}
 */
public class ConnectAsync {
    public static void main(String[] args) throws IOException {
        String host = "localhost";
        int port = 12344;
        if (args.length==2) {
            host = args[0];
            port = Integer.parseInt(args[1]);
        }
        InetSocketAddress address = new InetSocketAddress(host, port);
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        System.out.println("initiating connection!" );
        socketChannel.connect(address);
        while (!socketChannel.finishConnect()) {
            doSomething();
        }
        System.out.println("connection established");
        socketChannel.close();
    }

    private static void doSomething() {
        System.out.println("3333");
    }
}
